#!/bin/bash

# myconfig 全剧配置
[ -f "$HOME/myconfig" ] && . $HOME/myconfig        

declare -A usage
_info () {
    echo -e "\e[32m$@\e[39m"
}
_warn () {
    echo -e "\e[93m$@\e[39m"
}
_err () {
    echo -e "\e[31m$@\e[39m"
}


desc="c:centos u:ubuntu; ci:install on centos; ui:install on centos"
#script begin

#example
#description: 
usage[iu_docker]="install docker"
x(){
     _info "xx"
}
ipip_repo(){
  local confDir=$HOME/.pip
  [ ! -d $confDir ]  && mkdir $confDir
  cat ->$confDir/pip.conf <<EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF

}
ipip(){
    sudo apt-get install python-pip
    sudo apt-get install python3-pip
    ipip_repo
}
irepo(){
     sed -i -e 's/security.ubuntu.com/mirrors.aliyun.com/g' -e 's/cn.archive.ubuntu.com/mirrors.aliyun.com/g' -e 's/archive.ubuntu.com/mirrors.aliyun.com/g' -e '/mirrors.aliyuncs.com/d' /etc/apt/sources.list
}
idocker(){
   sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common 
   curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
   sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
   sudo apt-get -y update
   sudo apt-get -y install docker-ce
   sudo service docker start
}


func=$1
isHelp=$2
MY_DIR=$HOME/my
usage["_r"]="show in finder"
usage["_c"]="open in vs code"
_r(){
    open $MY_DIR/script
}
_c(){
    local editor="vim"
    [ ! -z "$(which subl)" ] && editor="subl"
    [ ! -z "$(which code)" ] && editor="code"
    $editor $MY_DIR/script
}
    if [ "$func" = "-" -o -z "$func" ];then
echo -e "$(_info $desc):\n"    
echo -e "$(_info usage):\n"
for key in "${!usage[@]}";do
    echo -e "$(_info $key): \n\t${usage[$key]}"
done

else


if [ "$(type -t $func)" = "function" ];then 

    shift 
    if [ "$isHelp" = "-" ];then 
        #usage="${func}_usage"
        #echo -e "$func usage:\n ${!usage}"
        echo -e "$(_info usage:\\n$func)\n  ${usage[$func]}"
        echo "option help"
    else
       $func "$@"
    fi

fi

fi

